<?php
class Line_price_profit_model extends CI_Model
{
    private $table   = 'line_price_profit';
    private $table_2 = 'user_type';
    
    public function findPriceProfit($data)
    {
        $this->db->select('*');
        $this->db->from($this->table.' AS line_price_profit');
        $this->db->join($this->table_2.' AS user_type','user_type.type_id=line_price_profit.bit_trade', 'inner');
        if (!empty($data['allot_id'])) {
           $this->db->where('line_price_profit.allot_id', $data['allot_id']);
        }
        if (!empty($data['status'])) { 
            $this->db->where('line_price_profit.status', $data['status']);
        }
        if (!empty($data['level'])){
            $this->db->where('line_price_profit.level', $data['level']);
        }
        $this->db->order_by('line_price_profit.level, line_price_profit.bit_trade ASC');
        return $this->db->get();
    }
    
    public function add($postData, $allot_id)
    {
        $arrayData = array();
        foreach ($postData['amount'] as $key=>$value) {   //线路成人
            if (intval($value) > 0) {
                $data = array(
                    'allot_id'  => $allot_id,
                    'level'     => trim($postData['level']),
                    'amount'    => trim($value),
                    'uid'       => trim($postData['uid'][$key]),
                    'status'    => trim($postData['status']),
                    'bit_trade' => trim($postData['type_id'][$key])
                );
                $arrayData[] = $data;
            }
        }
        if (isset($postData['children_amount']) && is_array($postData['children_amount'])) {   //线路  儿童
            foreach ($postData['children_amount'] as $key=>$value) {
                if (intval($value) > 0) {
                    $data = array(
                        'allot_id'  => $allot_id,
                        'level'     => trim($postData['children_level']),
                        'amount'    => trim($value),
                        'uid'       => trim($postData['children_uid'][$key]),
                        'status'    => trim($postData['status']),
                        'bit_trade' => trim($postData['children_type_id'][$key])
                    );
                    $arrayData[] = $data;
                }
            }
        }
        return $this->db->insert_batch($this->table, $arrayData);
    }
    
    public function delete($data, $id)
    {
        return $this->db->delete($this->table, array('allot_id' => $id)); 
    }
    
    public function bargainingDelete($allot_id, $status)
    {
        $this->db->where('allot_id', $allot_id);
        $this->db->where('status', $status);
        return $this->db->delete($this->table);
    }
}